#include <asl_core.h>
#include <asl_module.h>
#include <mod_curl.h>
#include <lib_curl.h>

MOD_ENT_FUNC_ARG_DEF(curl, get_url) = {
        MOD_ENT_FUNC_ARG_BODY("url", LEN("url"), value_type_string, 1, NULL),
        MOD_ENT_FUNC_EMPTY_ARG_BODY
};
MOD_ENT_FUNCS_DEF(curl) = {
        MOD_ENT_FUNC_BODY("get_curl_version", NULL, MOD_FUNC_NAME(get_curl_version)),
        MOD_ENT_FUNC_BODY("get_url", MOD_ENT_FUNC_ARG_NAME(curl, get_url), MOD_FUNC_NAME(get_url)),
        MOD_ENT_EMPTY_FUNC_BODY,
};

/**
 *
 */
MOD_MOD_BODY(curl);

/**
 * 初始化模块
 * @return
 */
MOD_INIT_FUNC_DEF(curl) {
    return RET_SUCCESS;
}

/**
 * 关闭模块
 * @return
 */
MOD_SHUTDOWN_FUNC_DEF(curl) {
    return RET_SUCCESS;
}

/**
 *
 * @param params
 * @return
 */
MOD_FUNC_DEF(get_curl_version) {
    char *s, *cs;
    cs = curl_version();
    s = (char *) memory_alloc(strlen(cs) + 1);
    strcpy(s, cs);
    return value_create_string(s);
}

/**
 *
 * @param params
 * @return
 */
MOD_FUNC_DEF(get_url) {
    value_t *url;
    lib_curl_mem_t *chunk;
    char *s, *rs;
    chunk = memory_alloc(sizeof(lib_curl_mem_t));
    chunk->memory = NULL;
    chunk->size = 0;
    s = (char *) memory_alloc(1024);
    chunk->memory = s;
    url = (value_t *) hash_find(var_table, "url", strlen("url"));
    lib_curl_get(url->u.str.val, chunk);
    rs = (char *) memory_alloc(chunk->size + 1);
    strcpy(rs, chunk->memory);
    memory_free(chunk->memory);
    memory_free(chunk);
    return value_create_string(rs);
}